﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ARS.PublicTool.Network
{
    public class HttpData
    {
        public HttpData()
        {
            //HttpData httpData=new HttpData();
            //httpData.GetData()
        }

        public  Action<WebResponse> httpDataGetAction;
        public   Action<HttpWebRequest> httpDataHeadAction;

        public  string dlIp;
        public  string WapGetData(String httpUrl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
            request.ServicePoint.Expect100Continue = false;
            request.Headers.Add("Q-AUTH", "da441402dfb3c8a00be11fb80bec66560d693a223f7ac56b");
            request.Headers.Add("Q-UA", "KQB11_GA/110065&KMTT_2/200065&JV2&152018&SonyEricssonK800&V2");
            request.Headers.Add("Q-GUID", "4b8115789cc15191168bd7e486c1f320");
            request.UserAgent = "SonyEricssonK800";
            //if (dlip != "")
            //{
            //    request.Proxy = new WebProxy(dlip, true);
            //}
            //        request.Proxy = new WebProxy(dlip2, true);
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
            string SourceCode = sr.ReadToEnd();
            resStream.Close();
            sr.Close();
            return SourceCode;
        }
        public  string WapPostData(String httpUrl,string postData)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return sr2.ReadToEnd();
        }

        public  string GetData(string httpUrl, string cookies = null, string refe = null, Encoding encode = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
            request.ServicePoint.Expect100Continue = false;
            if (httpDataHeadAction!=null)
            {
                httpDataHeadAction(request);
                httpDataHeadAction = null;
            }
            if (dlIp!=null)
            {
                request.Proxy = new WebProxy(dlIp, true);
            }
            if (cookies != null)
            {
                request.Headers.Add("Cookie", cookies);
            }
            if (refe != null)
            {
                request.Referer = refe;
            }

            WebResponse response = request.GetResponse();
            if (httpDataGetAction != null)
            {
                httpDataGetAction(response);
                httpDataGetAction = null;
            }
            Stream resStream = response.GetResponseStream();
            if (encode==null)
            {
                encode = Encoding.UTF8;
            }
            StreamReader sr = new StreamReader(resStream, encode);
            string SourceCode = sr.ReadToEnd();
            resStream.Close();
            sr.Close();
            return SourceCode;
        }


        public  string PostData(String httpUrl, string postData)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
            if (dlIp != null)
            {
                request.Proxy = new WebProxy(dlIp, true);
            }
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            if (httpDataHeadAction != null)
            {
                httpDataHeadAction(request);
                httpDataHeadAction = null;
            }
            Stream newStream = request.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return sr2.ReadToEnd();
        }

//        public  string PostDataGBK(String httpUrl, string postData)
//        {
//            byte[] byteArray = Encoding.Default.GetBytes(postData); // 转化
//            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
//            if (dlIp != null)
//            {
//                request.Proxy = new WebProxy(dlIp, true);
//            }
//            request.ServicePoint.Expect100Continue = false;
//            request.Method = "POST";
//            request.ContentType = "application/x-www-form-urlencoded";
//            request.ContentLength = byteArray.Length;
//            if (httpDataHeadAction != null)
//            {
//                httpDataHeadAction(request);
//                httpDataHeadAction = null;
//            }
//            Stream newStream = request.GetRequestStream();
//            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
//            newStream.Close();
//            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//            StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.Default);
//            return sr2.ReadToEnd();
//        }


        public  string PostData(string url, string postData, string cookies=null, string refe = null)
        
        {
           // System.Net.ServicePointManager.Expect100Continue = false;

            Encoding encode = Encoding.UTF8;
            byte[] byteArray = encode.GetBytes(postData); // 转化
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
            request.ServicePoint.Expect100Continue = false;
 
            request.Method = "POST";
            if (dlIp != null)
            {
                request.Proxy = new WebProxy(dlIp, true);
            }
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
            request.Accept = "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            if (cookies!=null)
            {
                request.Headers.Add("Cookie", cookies);
            }
            if (refe!=null)
            {
                request.Referer = refe;
            }
            if (httpDataHeadAction != null)
            {
                httpDataHeadAction(request);
                httpDataHeadAction = null;
            }
            Stream newStream = request.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr2 = new StreamReader(response.GetResponseStream(), encode);
            return sr2.ReadToEnd();
        }


        public  Stream GetStream(string httpUrl)
        {
            WebRequest request = WebRequest.Create(httpUrl);
            WebResponse response = request.GetResponse();

            if (httpDataGetAction != null)
            {
                httpDataGetAction(response);
            }

            Stream getStream = response.GetResponseStream();
            long contentLength = response.ContentLength;

            byte[] outBytes = new byte[contentLength];
            outBytes = ReadFully(getStream);
            getStream.Close();
            response.Close();
            getStream.Close();
            Stream stream = new MemoryStream(outBytes);
            return stream;
        }

        private  byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }

        public  byte[] GetFileBytes(string url)
        {
            WebResponse rsp = null;
            byte[] retBytes = null;
            try
            {
                Uri uri = new Uri(url);
                WebRequest req = WebRequest.Create(uri);
                rsp = req.GetResponse();
                Stream stream = rsp.GetResponseStream();
                //System.Windows.Forms.MessageBox.Show(rsp.Headers.Get("Content-Disposition"));

                if (httpDataGetAction != null)
                {
                    httpDataGetAction(rsp);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    int b;
                    while ((b = stream.ReadByte()) != -1)
                    {
                        ms.WriteByte((byte)b);
                    }
                    retBytes = ms.ToArray();
                }
            }
            catch (Exception ex)
            {
                retBytes = null;
            }
            finally
            {
                if (rsp != null)
                {
                    rsp.Close();
                }
            }
            return retBytes;
        }
    }    
}
